package com.example.mylibrary.client;

import com.example.mylibrary.exception.ExceptionEngine;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class HttpObservable {

    LifecycleProvider lifecycleProvider;
    //绑定Activity具体的生命周的
    ActivityEvent activityEvent;
    //绑定Fragment的具体的生命周期的
    FragmentEvent fragmentEvent;
    Observable observable;

    public HttpObservable(Buidler buidler) {
        this.lifecycleProvider=buidler.lifecycleProvider;
        this.activityEvent=buidler.activityEvent;
        this.fragmentEvent=buidler.fragmentEvent;
        this.observable=buidler.observable;
    }


    public Observable map(){
        return observable.map(new Function<JsonElement,Object>() {
            @Override
            public Object apply(JsonElement jsonElement) throws Exception {
                return new Gson().toJson(jsonElement);
            }
        });
    }


    public Observable onErrorResumeNext(){
        return map().onErrorResumeNext(new Function<Throwable, ObservableSource>() {
            @Override
            public ObservableSource apply(Throwable throwable) throws Exception {
                return Observable.error(ExceptionEngine.handleException(throwable));
            }
        });
    }

    public Observable bindToLifecycle(){
        Observable observable=null;
        if(lifecycleProvider!=null){
            if(activityEvent!=null||fragmentEvent!=null){
                if(activityEvent!=null&&fragmentEvent!=null){
                    observable=onErrorResumeNext().compose(lifecycleProvider.bindUntilEvent(activityEvent));
                }
                if(activityEvent!=null){
                    observable=onErrorResumeNext().compose(lifecycleProvider.bindUntilEvent(activityEvent));
                }

                if(fragmentEvent!=null){
                    observable=onErrorResumeNext().compose(lifecycleProvider.bindUntilEvent(fragmentEvent));
                }
            }else{
                observable=onErrorResumeNext().compose(lifecycleProvider.bindToLifecycle());
            }

        }else{
            observable=onErrorResumeNext();
        }

        return observable;
    }

    public Observable changThread(){

        return bindToLifecycle().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    public static class Buidler{
        LifecycleProvider lifecycleProvider;
        //绑定Activity具体的生命周的
        ActivityEvent activityEvent;
        //绑定Fragment的具体的生命周期的
        FragmentEvent fragmentEvent;
        Observable observable;

        public Buidler(Observable observable) {
            this.observable=observable;
        }

        public Buidler setLifecycleProvider(LifecycleProvider lifecycleProvider) {
            this.lifecycleProvider = lifecycleProvider;
            return this;
        }

        public Buidler setActivityEvent(ActivityEvent activityEvent) {
            this.activityEvent = activityEvent;
            return this;
        }

        public Buidler setFragmentEvent(FragmentEvent fragmentEvent) {
            this.fragmentEvent = fragmentEvent;
            return this;
        }

        public HttpObservable build(){

            return new HttpObservable(this);
        }
    }
}
